/*
 * @Author: FredWang
 * @Date: 2019-01-14 14:52:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2019-02-15 12:34:55
 * @Description: 
 */

const arrayNumber = function(str) {
    let num = str.toString()
    let array = []
    for (var i = 1; i <= 5; i++) {
        if (i <= num) {
            array.push(true)
        } else {
            array.push(false)
        }
    }
    return array
}
const filters = {
    dateForm(date) {
        return new Date(date).toLocaleString('chinese', { hour12: false })
    }
}

const DateFormat = (date, fmt) => {
    var dateObj = {
        "M+": date.getMonth() + 1, //月份
        "D+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "S": date.getMilliseconds() //毫秒
    };
    if (/(Y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in dateObj)
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (dateObj[k]) : (("00" + dateObj[k]).substr(("" + dateObj[k]).length)));
        }
    return fmt;
}

// 将整数转换为浮点数
const ToFloat = function(num, n = 1) {
    let str = String(num)
    if (str.includes('.')) {
        let len = str.split('.')[1].length
        if (len < n) {
            return str + '0'.repeat(n - len)
        }
        return str
    } else {
        return str + '.' + '0'.repeat(n)
    }
}
// 随机字符串
const RandomStr = function() {
    return Math.random().toString(36).substr(2);
}


// Changes XML to JSON
const xmlToJson = function(xml) {
     
    // Create the return object
    var obj = {};
 
    if (xml.nodeType == 1) { // element
        // do attributes
        if (xml.attributes.length > 0) {
        obj["@attributes"] = {};
            for (var j = 0; j < xml.attributes.length; j++) {
                var attribute = xml.attributes.item(j);
                obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
            }
        }
    } else if (xml.nodeType == 3) { // text
        obj = xml.nodeValue;
    }
 
    // do children
    if (xml.hasChildNodes()) {
        for(var i = 0; i < xml.childNodes.length; i++) {
            var item = xml.childNodes.item(i);
            var nodeName = item.nodeName;
            if (typeof(obj[nodeName]) == "undefined") {
                obj[nodeName] = xmlToJson(item);
            } else {
                if (typeof(obj[nodeName].length) == "undefined") {
                    var old = obj[nodeName];
                    obj[nodeName] = [];
                    obj[nodeName].push(old);
                }
                obj[nodeName].push(xmlToJson(item));
            }
        }
    }
    return obj;
}
// 找到数组中第一个符合条件的元素
const ArrFilter = function(arr, callBack) {
    for (let i = 0; i < arr.length; i++) {
        if (callBack(arr[i])) {
            return i
        }
    }
    return -1
}
export {
    arrayNumber,
    DateFormat,
    filters,
    ToFloat,
    RandomStr,
    xmlToJson,
    ArrFilter
}